﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using NotAClue;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web;
using System.Web.Security;

namespace NotAClue.Web.DynamicData
{
    /// <summary>
    /// Renders templates for ListView
    /// </summary>
    public class ListViewTemplateGenerator : ITemplate
    {
        #region Member Fields, Constructor, Enums & Properties
        private const string EnumerationField = "Enumeration";
        private const string CELL_PADDING = "6";
        private const string DD_FOOTER = "DDFooter";

        private const string INSERT = "Insert";
        private const string UPDATE = "Update";
        private const string EDIT = "Edit";
        private const string REFRESH = "Refresh";
        private const string CANCEL = "Cancel";
        private const string DELETE = "Delete";
        private const string DETAILS = "Details";

        private const String ALT_INSERT = "Save";
        private const String ALT_UPDATE = "Save";
        private const String ALT_EDIT = "Edit";
        private const String ALT_REFRESH = "Refresh";
        private const String ALT_CANCEL = "Cancel";
        private const String ALT_DELETE = "Delete";
        private const String ALT_DETAILS = "Details";

        private MetaTable _table;
        private Page _page;
        private ListViewTemplateType _type;
        private CommandButtons _enabledButtons;
        private String _validationGroup;
        private Boolean _showPager;
        private Boolean _addFooter;
        private String _cssClass;
        private String[] _hideColumns;
        private String _imageFolderBase = "~/DynamicData/Content/Images/";
        private Boolean _showDetailedNoRecordsMessage;
        private CommandCellPosition _commandCellPosition;
        private Boolean _wrapOn;

        // flags
        private bool _anyButtonsVisible;

        public ListViewTemplateGenerator(
            Page page,
            MetaTable table,
            ListViewTemplateType type,
            CommandButtons enabledButtons,
            String validationGroup,
            Boolean showPager,
            Boolean addFooter,
            String cssClass,
            String[] hideColumns,
            Boolean showDetailedNoRecordsMessage,
            CommandCellPosition commandCellPosition,
            Boolean wrapOn)
        {
            _page = page;
            _table = table;
            _type = type;
            _enabledButtons = enabledButtons;
            _validationGroup = validationGroup;
            _showPager = showPager;
            _addFooter = addFooter;
            _cssClass = cssClass;
            _hideColumns = hideColumns ?? new String[0];
            _showDetailedNoRecordsMessage = showDetailedNoRecordsMessage;
            _commandCellPosition = commandCellPosition;
            _wrapOn = wrapOn;
        }
        #endregion

        public void InstantiateIn(Control container)
        {
            IParserAccessor accessor = container;

            var pageTemplate = _page.GetPageTemplate();
            if (pageTemplate == PageTemplate.DetailsList || pageTemplate == PageTemplate.EditList)
                pageTemplate = PageTemplate.List;

            var columnDetails = ((SecureMetaTable)_table).GetScaffoldColumns(DataBoundControlMode.ReadOnly, ContainerType.List, pageTemplate, _hideColumns);

            SetAnyVisibleButtonBySecurity();
            // call the appropriate template generator
            switch (_type)
            {
                case ListViewTemplateType.LayoutTemplate:
                    GetLayoutTemplate(accessor, columnDetails);
                    break;
                case ListViewTemplateType.ItemTemplate:
                    GetItemTemplate(accessor, ListViewTemplateMode.Normal, columnDetails);
                    break;
                case ListViewTemplateType.AlternatingItemTemplate:
                    GetItemTemplate(accessor, ListViewTemplateMode.Alternate, columnDetails);
                    break;
                case ListViewTemplateType.EditItemTemplate:
                    GetItemTemplate(accessor, ListViewTemplateMode.Edit, columnDetails);
                    break;
                case ListViewTemplateType.InsertItemTemplate:
                    GetItemTemplate(accessor, ListViewTemplateMode.Insert, columnDetails);
                    break;
                case ListViewTemplateType.SelectedItemTemplate:
                    GetItemTemplate(accessor, ListViewTemplateMode.Selected, columnDetails);
                    break;
                case ListViewTemplateType.GroupTemplate:
                    GetGroupTemplate(accessor);
                    break;
                case ListViewTemplateType.GroupSeparatorTemplate:
                    GetGroupSeparatorTemplate(accessor);
                    break;
                case ListViewTemplateType.ItemSeparatorTemplate:
                    GetItemSeparatorTemplate(accessor);
                    break;
                case ListViewTemplateType.EmptyDataTemplate:
                    GetEmptyDataTemplate(accessor);
                    break;
                case ListViewTemplateType.EmptyItemTemplate:
                    GetEmptyItemTemplate(accessor);
                    break;
                default:
                    break;
            }
        }

        private void SetAnyVisibleButtonBySecurity()
        {
            var commandButtonActions = new List<TableActions>()
                                {
                                    TableActions.Delete,
                                    TableActions.Details,
                                    TableActions.Edit,
                                };

            if (!_enabledButtons.HasAnyButton(CommandButtons.Details))
                commandButtonActions.Remove(TableActions.Details);

            foreach (var action in commandButtonActions)
            {
                if (_table.HasTableAction(action))
                    _anyButtonsVisible = true;
            }
        }

        private void GetLayoutTemplate(IParserAccessor accessor, IEnumerable<MetaColumn> columnDetails)
        {
            // make sure there are some children columns
            if (columnDetails.Count() > 0)
            {
                // HtmlTable/HtmlTableRow/HtmlTableCell 
                // create table
                var htmlTable = new HtmlTable();
                htmlTable.Attributes.Add("cellspacing", "0");
                htmlTable.Attributes.Add("cellpadding", CELL_PADDING);
                htmlTable.Attributes.Add("rules", "all");
                htmlTable.Attributes.Add("border", "1");
                htmlTable.Attributes.Add("class", _cssClass);

                // add table to accessor
                accessor.AddParsedSubObject(htmlTable);

                // set commandColumnNo to 0 and then 1 if there is a command column
                var CommandColumnNo = (_anyButtonsVisible && _enabledButtons.HasAnyButton(CommandButtons.Delete, CommandButtons.Details, CommandButtons.Edit, CommandButtons.Insert)) ? 1 : 0;

                var commandRow = CreateCommandRow(columnDetails.Count(), CommandColumnNo);
                if (commandRow != null)
                    htmlTable.Rows.Add(commandRow);

                // create header row
                var headerRow = new HtmlTableRow();
                headerRow.Attributes.Add("class", "th");

                if (CommandColumnNo == 1 && _commandCellPosition == CommandCellPosition.Left)
                {
                    // create empty header cell
                    var commandCell = new HtmlTableCell("th");
                    headerRow.Cells.Add(commandCell);
                }

                // add a column heading for each column
                foreach (var column in columnDetails)
                {
                    //TODO fix sorting on MetaForeignKeyColumn
                    var cell = new HtmlTableCell("th");

                    // add css
                    var css = column.GetAttribute<CellCssAttribute>();
                    if (css != null)
                        cell.SetAttributeClass("class", css.Class);

                    var linkButton = new LinkButton()
                    {
                        ID = column.Name + "LinkButton",
                        CommandName = "Sort",
                        CommandArgument = column.Name,
                        Text = column.DisplayName,
                        //CssClass = "DDControl",
                    };

                    if (column is MetaForeignKeyColumn)
                    {
                        var mfkColumn = column as MetaForeignKeyColumn;
                        var keyNames = new StringBuilder();

                        foreach (var keyName in mfkColumn.ForeignKeyNames)
                        {
                            keyNames.Append(keyName + ",");
                        }
                        linkButton.CommandArgument = keyNames.ToString().Substring(0, keyNames.Length - 1);
                    }

                    cell.Controls.Add(linkButton);
                    headerRow.Cells.Add(cell);
                }

                if (CommandColumnNo == 1 && _commandCellPosition == CommandCellPosition.Right)
                {
                    // create empty header cell
                    var commandCell = new HtmlTableCell("th");
                    headerRow.Cells.Add(commandCell);
                }

                htmlTable.Rows.Add(headerRow);

                //<tbody>
                //    <tr id="itemPlaceHolder" runat="server">
                //    </tr>
                //</tbody>
                // create the table body and item place holder
                var itemPlaceholder = new HtmlTableRow();
                itemPlaceholder.Attributes.Add("class", "td");
                itemPlaceholder.ID = "itemPlaceholder";
                itemPlaceholder.Attributes.Add("runat", "Server");
                htmlTable.Rows.Add(itemPlaceholder);

                if (_showPager)
                {
                    //<tfoot>
                    //    <tr class="DDFooter">
                    //        <td colspan="14">
                    //            <asp:ListViewPager ID="ListViewDataPager" runat="server"></asp:ListViewPager>
                    //        </td>
                    //    </tr>
                    //</tfoot>
                    // create the footer
                    var footerRow = new HtmlTableRow();
                    footerRow.Attributes.Add("class", DD_FOOTER);

                    // set column span to number of columns
                    // plus 1 to account for the command column
                    var footerCell = new HtmlTableCell();

                    // set column span
                    footerCell.ColSpan = columnDetails.Count() + CommandColumnNo;

                    // get the path to the ListViewPager
                    var listViewPagerPath = _table.Model.DynamicDataFolderVirtualPath + "Content/ListViewPager.ascx";
                    // load ListViewPager control
                    var listViewPager = _page.LoadControl(listViewPagerPath);
                    listViewPager.ID = "ListViewDataPager";

                    footerCell.Controls.Add(listViewPager);
                    footerRow.Cells.Add(footerCell);
                    htmlTable.Rows.Add(footerRow);
                }

                if (_addFooter)
                {
                    var footerRow = new HtmlTableRow();
                    footerRow.Attributes.Add("class", "totals");

                    if (CommandColumnNo == 1 && _commandCellPosition == CommandCellPosition.Left)
                    {
                        var commandCell = new HtmlTableCell("td");
                        footerRow.Cells.Add(commandCell);
                    }

                    // add a column heading for each column
                    foreach (var column in columnDetails)
                    {
                        // fix sorting on MetaForeignKeyColumn
                        var cell = new HtmlTableCell("td");

                        // add css
                        var css = column.GetAttribute<CellCssAttribute>();
                        if (css != null)
                            cell.SetAttributeClass("class", css.Class);

                        var litPlaceHolder = new Literal()
                        {
                            ID = column.Name + "PlaceHolder",
                            Text = "0"
                        };

                        cell.Controls.Add(litPlaceHolder);
                        footerRow.Cells.Add(cell);
                    }

                    if (_anyButtonsVisible && _enabledButtons.HasAnyButton(CommandButtons.Delete, CommandButtons.Details, CommandButtons.Edit) && _commandCellPosition == CommandCellPosition.Right)
                    {
                        var commandCell = new HtmlTableCell("td");
                        footerRow.Cells.Add(commandCell);
                    }

                    htmlTable.Rows.Add(footerRow);
                }

            }
            // if there are no children columns don't
            // bother to set the accessor to anything
        }

        private void GenerateCommandCell(ListViewTemplateMode templateMode, string validationGroup, Literal litSpacer, HtmlTableRow row)
        {
            if (_anyButtonsVisible && _enabledButtons.HasAnyButton(CommandButtons.Delete, CommandButtons.Details, CommandButtons.Edit, CommandButtons.Insert))
            {
                // create the cell to hold the command buttons
                // NOTE: you could move the command cell to after the
                //       for each column generator code if you wanted
                //       the command buttons to be at the end of the row
                //       you would also need to modify the LayoutTemplate code
                var commandCell = new HtmlTableCell();
                if (_wrapOn)
                    commandCell.Attributes.Add("class", "nowrap");
                row.Cells.Add(commandCell);

                switch (templateMode)
                {
                    case ListViewTemplateMode.Edit:
                        //Update Link
                        var lbUpdate = new SecureLinkButton()
                        {
                            ID = "UpdateLinkButton",
                            CommandName = UPDATE,
                            Text = ALT_UPDATE,
                            CssClass = "DDControl",
                            //ToolTip = "Update current action"
                        };
                        if (!String.IsNullOrEmpty(_validationGroup))
                            lbUpdate.ValidationGroup = validationGroup;

                        commandCell.Controls.Add(lbUpdate);
                        commandCell.Controls.Add(litSpacer);

                        //Cancel Link
                        var lbCancel = new SecureLinkButton()
                        {
                            //ID = "UpdateLinkButton",
                            CommandName = CANCEL,
                            Text = ALT_CANCEL,
                            CssClass = "DDControl",
                            //ToolTip = "Cancel current action"
                        };
                        commandCell.Controls.Add(lbCancel);
                        break;
                    case ListViewTemplateMode.Insert:

                        //Insert Link
                        var lkbInsert = new SecureLinkButton()
                        {
                            //ID = "InsertLinkButton",
                            CommandName = INSERT,
                            Text = ALT_INSERT,
                            CssClass = "DDControl",
                            //ToolTip = "Insert new record"
                        };
                        if (!String.IsNullOrEmpty(_validationGroup))
                            lkbInsert.ValidationGroup = validationGroup;

                        commandCell.Controls.Add(lkbInsert);
                        commandCell.Controls.Add(litSpacer);

                        //Cancel Link
                        var lkbCancel = new SecureLinkButton()
                        {
                            //ID = "CancelLinkButton",
                            CommandName = CANCEL,
                            Text = ALT_CANCEL,
                            CssClass = "DDControl",
                            //ToolTip = "Cancel current action",
                            CausesValidation = false,
                        };
                        commandCell.Controls.Add(lkbCancel);
                        break;
                    case ListViewTemplateMode.Selected:
                        row.Attributes.Add("class", "selected");
                        goto case ListViewTemplateMode.Normal;
                    case ListViewTemplateMode.Alternate:
                        row.Attributes.Add("class", "alternate");
                        goto case ListViewTemplateMode.Normal;
                    case ListViewTemplateMode.Normal:

                        if (_enabledButtons.HasAnyButton(CommandButtons.Details))
                        {
                            var dynamicHyperLink = new DynamicHyperLink()
                            {
                                ID = "DetailsLinkButton",
                                Text = DETAILS,
                                CssClass = "DDControl",
                                //ImageUrl = _imageFolderBase + "Details.png",
                                //ToolTip = "Show " + DETAILS
                            };

                            commandCell.Controls.Add(dynamicHyperLink);
                            commandCell.Controls.Add(litSpacer);
                        }

                        if (_enabledButtons.HasAnyButton(CommandButtons.Edit))
                        {
                            //Edit Link
                            var lkbEdit = new SecureLinkButton()
                            {
                                //ID = "EditLinkButton",
                                CommandName = EDIT,
                                Text = ALT_EDIT,
                                CssClass = "DDControl",
                                //ToolTip = "Edit current record",
                                CausesValidation = false,
                            };
                            commandCell.Controls.Add(lkbEdit);
                            commandCell.Controls.Add(litSpacer);
                        }

                        if (_enabledButtons.HasAnyButton(CommandButtons.Delete))
                        {
                            //Delete Link
                            var lkbDelete = new SecureLinkButton()
                            {
                                //ID = "DeleteLinkButton",
                                CommandName = DELETE,
                                Text = ALT_DELETE,
                                CausesValidation = true,
                                CssClass = "DDControl",
                                OnClientClick = "return confirm(\"Are you sure you want to delete this item?\");",
                            };
                            commandCell.Controls.Add(lkbDelete);
                            if (!String.IsNullOrEmpty(_validationGroup))
                                lkbDelete.ValidationGroup = validationGroup;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetItemTemplate(IParserAccessor accessor, ListViewTemplateMode templateMode, IEnumerable<MetaColumn> columnDetails)
        {
            var mode = DataBoundControlMode.ReadOnly;
            switch (templateMode)
            {
                case ListViewTemplateMode.Edit:
                    mode = DataBoundControlMode.Edit;
                    break;
                case ListViewTemplateMode.Insert:
                    mode = DataBoundControlMode.Insert;
                    break;
                case ListViewTemplateMode.Normal:
                case ListViewTemplateMode.Alternate:
                case ListViewTemplateMode.Selected:
                default:
                    break;
            }

            // make sure there are some children columns
            if (columnDetails.Count() > 0)
            {
                //// set the dynamic control mode to read only
                //DataBoundControlMode mode = DataBoundControlMode.ReadOnly;

                var validationGroup = String.Format("{0}_{1}", _validationGroup, templateMode);

                // create a spacer
                var litSpacer = new Literal();
                litSpacer.Text = @"&nbsp;";

                // create new row for template
                var row = new HtmlTableRow();
                row.Attributes.Add("class", "td");

                // add row to accessor
                accessor.AddParsedSubObject(row);

                if (_commandCellPosition == CommandCellPosition.Left)
                    GenerateCommandCell(templateMode, validationGroup, litSpacer, row);

                // add a cell for each column in table
                foreach (var column in columnDetails)
                {
                    // create new cell
                    var cell = new HtmlTableCell();

                    // add css
                    var css = column.GetAttribute<CellCssAttribute>();
                    if (css != null)
                        cell.SetAttributeClass("class", css.Class);

                    // add cell to row
                    row.Cells.Add(cell);

                    // instantiate a DynamicControl for this Children Column
                    var lvColumn = new DynamicControl(mode)
                    {
                        ID = column.Name,
                        // set data field to column name
                        DataField = column.Name,
                        UIHint = column.ConditionalUIHint(_page.GetPageTemplate())
                    };

                    if (!String.IsNullOrEmpty(_validationGroup))
                        lvColumn.ValidationGroup = validationGroup;

                    //if (column.ColumnType.IsEnum)
                    //    lvColumn.UIHint = EnumerationField;

                    //if (column.IsForeignKeyComponent || column.ColumnType == typeof(MetaChildrenColumn))
                    //    lvColumn.AllowNavigation = false;

                    // add control to cell
                    cell.Controls.Add(lvColumn);
                }

                if (_commandCellPosition == CommandCellPosition.Right)
                    GenerateCommandCell(templateMode, validationGroup, litSpacer, row);
            }
        }

        private void GetEmptyDataTemplate(IParserAccessor accessor)
        {
            // HtmlTable/HtmlTableRow/HtmlTableCell 
            // create table
            var htmlTable = new HtmlTable();
            htmlTable.Attributes.Add("cellspacing", "0");
            htmlTable.Attributes.Add("cellpadding", CELL_PADDING);
            htmlTable.Attributes.Add("rules", "all");
            htmlTable.Attributes.Add("border", "1");
            htmlTable.Attributes.Add("class", _cssClass);

            // add table to accessor
            accessor.AddParsedSubObject(htmlTable);


            // set commandColumnNo to 0 and then 1 if there is a command column
            var CommandColumnNo = _enabledButtons.HasAnyButton(CommandButtons.Delete, CommandButtons.Details, CommandButtons.Edit, CommandButtons.Insert) ? 1 : 0;
            var commandRow = CreateCommandRow(1, CommandColumnNo);
            if (commandRow != null)
                htmlTable.Rows.Add(commandRow);

            // create new row for template
            var row = new HtmlTableRow();
            row.Attributes.Add("class", "td");

            // add row to htmlTable rows
            htmlTable.Rows.Add(row);

            // create new cell
            var cell = new HtmlTableCell();
            cell.ColSpan = 2;

            // add cell to row
            row.Cells.Add(cell);

            // add empty template message
            if (_showDetailedNoRecordsMessage)
                cell.InnerText = @"There are no " + _table.DataContextPropertyName.ToTitleFromPascal();
            else
                cell.InnerText = "No records found";
        }

        private void GetEmptyItemTemplate(IParserAccessor accessor)
        {
            var mode = DataBoundControlMode.ReadOnly;
            var columnDetails = _table.GetScaffoldColumns(mode, ContainerType.List);
            if (columnDetails.Count() > 0)
            {

                // create a spacer
                var litSpacer = new Literal();
                litSpacer.Text = @"&nbsp;";

                // create new row for template
                var row = new HtmlTableRow();
                row.Attributes.Add("class", "td");

                // add row to accessor
                accessor.AddParsedSubObject(row);

                if (_anyButtonsVisible && _enabledButtons.HasAnyButton(CommandButtons.Delete, CommandButtons.Details, CommandButtons.Edit, CommandButtons.Insert))
                {
                    var commandCell = new HtmlTableCell();
                    if (_wrapOn)
                        commandCell.Attributes.Add("class", "nowrap");
                    row.Cells.Add(commandCell);
                }

                // add a cell for each column in table
                foreach (var column in columnDetails)
                {
                    // create new cell
                    var cell = new HtmlTableCell();

                    // add css
                    var css = column.GetAttribute<CellCssAttribute>();
                    if (css != null)
                        cell.SetAttributeClass("class", css.Class);

                    // add cell to row
                    row.Cells.Add(cell);
                }
            }
        }

        /// <summary>
        /// Creates the command row.
        /// </summary>
        /// <param name="columnCount">The column count.</param>
        /// <returns></returns>
        public HtmlTableRow CreateCommandRow(int columnCount, int commandButtons)
        {
            if (!_enabledButtons.HasAnyButton(CommandButtons.Insert, CommandButtons.Refresh))
                return null;

            if (!_table.HasTableAction(TableActions.Insert) && !_enabledButtons.HasAnyButton(CommandButtons.Refresh))
                return null;

            var commandRow = new HtmlTableRow();
            commandRow.Attributes.Add("class", "th");

            // get total columns
            var cols = columnCount + commandButtons;

            // get number of commands
            var insertCells = (_table.HasTableAction(TableActions.Insert) && _enabledButtons.HasAnyButton(CommandButtons.Insert) ? 1 : 0);
            var refreshCells = (_enabledButtons.HasAnyButton(CommandButtons.Refresh) ? 1 : 0);
            var commands = insertCells + refreshCells;

            // get column span per button
            var cellSpan = cols / commands;
            // get padding
            var padding = cols % commands != 0 ? 1 : 0;

            if (_table.HasTableAction(TableActions.Insert) && _enabledButtons.HasAnyButton(CommandButtons.Insert))
            {
                var insertCell = new HtmlTableCell("th");
                insertCell.Attributes.Add("class", "InsertHeader");
                insertCell.ColSpan = cellSpan + padding;
                insertCell.Controls.Add(CreateLinkWithImage("ShowInsert", String.Format("Add {0}", _table.DisplayName)));
                commandRow.Cells.Add(insertCell);
            }

            if (_enabledButtons.HasAnyButton(CommandButtons.Refresh))
            {
                var refreshCell = new HtmlTableCell("th");
                refreshCell.ColSpan = cellSpan;
                refreshCell.Attributes.Add("class", "RefreshHeader");
                refreshCell.Controls.Add(CreateLinkWithImage(REFRESH, "Refresh"));
                commandRow.Cells.Add(refreshCell);
            }

            return commandRow;
        }

        /// <summary>
        /// Creates the link with image.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="command">if set to <c>true</c> [command].</param>
        /// <returns></returns>
        public Control CreateLinkWithImage(String action, String message)
        {
            var placeHolder = new PlaceHolder();
            var displayText = String.Format("<img src='{0}.png' border='0' /> {1}", _page.ResolveUrl(_imageFolderBase + action), message);

            var linkButton = new SecureLinkButton();
            linkButton.CssClass = "DDControl";
            linkButton.Text = displayText;
            linkButton.CommandName = action;
            linkButton.CausesValidation = false;
            return linkButton;
        }

        ///// <summary>
        ///// Creates the link with image.
        ///// </summary>
        ///// <param name="action">The action.</param>
        ///// <param name="command">if set to <c>true</c> [command].</param>
        ///// <returns></returns>
        //public Control CreateLinkWithImage(String action, String message)
        //{
        //    var placeHolder = new PlaceHolder();
        //    var displayText = String.Format("{0}", message);

        //    // new image button
        //    var imageButton = new ImageButton();
        //    imageButton.CssClass = "InsertImage";
        //    imageButton.ImageUrl = String.Format("{0}{1}.png", _imageFolderBase, action);
        //    imageButton.ToolTip = displayText;
        //    imageButton.CommandName = action;
        //    placeHolder.Controls.Add(imageButton);

        //    var spacer = new Literal();
        //    spacer.Text = "&nbsp;";
        //    placeHolder.Controls.Add(spacer);

        //    var linkButton = new SecureLinkButton();
        //    linkButton.CssClass = "DDControl";
        //    linkButton.Text = displayText;
        //    linkButton.CommandName = action;
        //    linkButton.CausesValidation = false;
        //    //return linkButton;
        //    placeHolder.Controls.Add(linkButton);

        //    return placeHolder;
        //}

        #region not implemented
        private void GetGroupTemplate(IParserAccessor accessor)
        {
            throw new NotImplementedException();
        }

        private void GetGroupSeparatorTemplate(IParserAccessor accessor)
        {
            throw new NotImplementedException();
        }

        private void GetItemSeparatorTemplate(IParserAccessor accessor)
        {
            throw new NotImplementedException();
        }
        #endregion
    }

    public static partial class HelperExtensionMethods
    {
        //public static void GetTemplates(this ListView listView, MetaTable table)
        //{
        //    listView.GetTemplates(table, Constants.ListViewButtons, "", true, false, "DDGridView", null);
        //}

        //public static void GetTemplates(this ListView listView, MetaTable table, String validationGroup, Boolean showPager, CommandButtons enabledButtons, Boolean addFooter)
        //{
        //    listView.GetTemplates(table, enabledButtons, validationGroup, showPager, addFooter, "DDGridView", null);
        //}

        //public static void GetTemplates(this ListView listView, MetaTable table, CommandButtons enabledButtons)
        //{
        //    listView.GetTemplates(table, enabledButtons, "", true, false, "DDGridView", null);
        //}

        //public static void GetTemplates(this ListView listView, 
        //    MetaTable table, 
        //    CommandButtons enabledButtons, 
        //    String validationGroup, 
        //    String cssClass, 
        //    String[] hideColumns, 
        //    Boolean showDetailedNoRecordsMessage)
        //{
        //    listView.GetTemplates(table, enabledButtons, validationGroup, true, false, cssClass, hideColumns, showDetailedNoRecordsMessage);
        //}

        public static void GetTemplates(this ListView listView,
            MetaTable table,
            CommandButtons enabledButtons,
            String validationGroup,
            String cssClass,
            String[] hideColumns,
            Boolean showDetailedNoRecordsMessage = false,
            Boolean showPager = true,
            Boolean addFooter = false,
            CommandCellPosition commandCellPosition = CommandCellPosition.Left,
            Boolean wrapOn = true)
        {
            // load layout template
            listView.LayoutTemplate = new ListViewTemplateGenerator(listView.Page,
                                          table,
                                          ListViewTemplateType.LayoutTemplate,
                                          enabledButtons,
                                          validationGroup,
                                          showPager,
                                          addFooter,
                                          cssClass,
                                          hideColumns,
                                          showDetailedNoRecordsMessage,
                                          commandCellPosition,
                                          wrapOn);

            // load item template
            listView.ItemTemplate = new ListViewTemplateGenerator(listView.Page,
                                        table,
                                        ListViewTemplateType.ItemTemplate,
                                        enabledButtons,
                                        validationGroup,
                                        showPager,
                                        addFooter,
                                        cssClass,
                                        hideColumns,
                                        showDetailedNoRecordsMessage,
                                        commandCellPosition,
                                        wrapOn);

            // load edit template
            listView.EditItemTemplate = new ListViewTemplateGenerator(listView.Page,
                                            table,
                                            ListViewTemplateType.EditItemTemplate,
                                            enabledButtons,
                                            validationGroup,
                                            showPager,
                                            addFooter,
                                            cssClass,
                                            hideColumns,
                                            showDetailedNoRecordsMessage,
                                            commandCellPosition,
                                            wrapOn);

            // load insert template
            listView.InsertItemTemplate = new ListViewTemplateGenerator(listView.Page,
                                              table,
                                              ListViewTemplateType.InsertItemTemplate,
                                              enabledButtons,
                                              validationGroup,
                                              showPager,
                                              addFooter,
                                              cssClass,
                                              hideColumns,
                                              showDetailedNoRecordsMessage,
                                              commandCellPosition,
                                              wrapOn);

            // set the location of the insert row
            listView.InsertItemPosition = InsertItemPosition.FirstItem;

            // load empty template
            listView.EmptyDataTemplate = new ListViewTemplateGenerator(listView.Page,
                                             table,
                                             ListViewTemplateType.EmptyDataTemplate,
                                             enabledButtons,
                                             validationGroup,
                                             showPager,
                                             addFooter,
                                             cssClass,
                                             hideColumns,
                                             showDetailedNoRecordsMessage,
                                             commandCellPosition,
                                             wrapOn);

            //listView.AlternatingItemTemplate = null;
            //listView.EmptyItemTemplate = null;
            //listView.GroupSeparatorTemplate = null;
            //listView.GroupTemplate = null;
            //listView.ItemSeparatorTemplate = null;
            //listView.SelectedItemTemplate = null;
        }
    }
}
